import os
from pdf2image import convert_from_path
# 首先用这个程序，将pdf文件转换为png图像

def judge_folder_exists(folder_path):

    if not os.path.exists(folder_path):
        os.mkdir(folder_path)


def pdf2png(pdf_path_ls):

    for pdf_path in pdf_path_ls:

        output_path = ''

        convert_from_path(pdf_path, output_folder=output_path, fmt="png",
                          size=(None, 3300), use_cropbox=True)


def main():

    # root_path = os.getcwd() + os.sep + "PDFCandy_PDF2RTF"
    root_path = os.getcwd() + os.sep + 'input' + os.sep + 'test'
    file_ls = os.listdir(root_path)

    for file in file_ls:

        if '.pdf' == file[-4:]:

            pdf_file_path = root_path + os.sep + file
            output_path = root_path.replace('test', 'png') + os.sep + file.replace(".pdf", "")
            judge_folder_exists(output_path)
            convert_from_path(pdf_file_path, output_folder=output_path, fmt="png",
                              size=(None, 3300), use_cropbox=True)


if __name__ == '__main__':
    main()